package com.zmn.oms.services.impl.work;

import com.google.common.collect.Lists;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.cube.common.utils.StringUtil;
import com.zmn.oms.common.annotation.OrderWorkChangeSync;
import com.zmn.oms.common.constant.OrderDutyConsts;
import com.zmn.oms.common.utils.EmojiUtil;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.persistence.interfaces.work.OrderWorkDao;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import io.shardingsphere.api.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 类描述：工单
 *
 * @author liuying
 * @date 2018/11/08 15:41
 */
@Service
public class OrderWorkServiceImpl implements OrderWorkService {

    @Autowired
    private OrderWorkDao orderWorkDao;

    @Override
    public List<OrderWork> listEsOrderWorkByOrderIdList(List<Long> workIdList) {

        if (CollectionUtils.isEmpty(workIdList)) {
            return Lists.newArrayList();
        }

        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderWorkDao.listEsOrderWorkByOrderIdList(workIdList);
        }
    }

    @Override
    public List<OrderWork> listOrderWorkByOrderIdList(List<Long> workIdList) {

        if (CollectionUtils.isEmpty(workIdList)) {
            return Lists.newArrayList();
        }
        return orderWorkDao.listEsOrderWorkByOrderIdList(workIdList);
    }

    @Override
    public OrderWork findOrderWorkByKeySrcMaster(Long orderId, Long workId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return findOrderWorkByKey(orderId, workId);
        }
    }

    @Override
    public OrderWork findOrderWorkByKey(Long orderId, Long workId) {
        return orderWorkDao.findOrderWorkByKey(orderId, workId);
    }


    @Override
    public OrderWork findDynamicOrderWorkByKey(String querySql, Long orderId, Long workId) {
        return orderWorkDao.findDynamicOrderWorkByKey(querySql, orderId, workId);
    }

    @Override
    public OrderWork findMasterWorkDetailSrcMaster(Long orderId, Long workId, Integer masterId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return findMasterWorkDetail(orderId, workId, masterId);
        }
    }

    @Override
    public OrderWork findMasterWorkDetail(Long orderId, Long workId, Integer masterId) {
        return orderWorkDao.findMasterWorkDetail(orderId, workId, masterId);
    }

    @Override
    public void updateChannel(OrderWork orderWork) {
        orderWorkDao.updateChannel(orderWork);
    }

    @Override
    public void saveDistribute(OrderWork orderWork) {
        orderWorkDao.updateDistribute(orderWork);
    }

    @Override
    public void saveMultiMaster(OrderWork orderWork) {
        orderWorkDao.updateMultiMaster(orderWork);
    }

    @Override
    public void updateCancel(OrderWork updateOrderWork) {
        orderWorkDao.updateCancel(updateOrderWork);
    }

    @Override
    public void updateAbnormalStatus(OrderWork orderWork) {
        orderWorkDao.updateAbnormalStatus(orderWork);
    }

    @Override
    public void updateComplaintStatus(OrderWork orderWork) {
        orderWorkDao.updateComplaintStatus(orderWork);
    }

    @Override
    public void updateOrderChannelSettleStatus(OrderWork orderWork) {
        orderWorkDao.updateOrderChannelSettleStatus(orderWork);
    }

    @Override
    public void updateWorkRemark(OrderWork orderWork) {
        if (StringUtil.isNotBlank(orderWork.getCurrentRemark())){
            orderWork.setCurrentRemark(EmojiUtil.filterEmoji(orderWork.getCurrentRemark()));
        }
        orderWorkDao.updateWorkRemark(orderWork);
    }

    @Override
    public void updateConfirmToInputStatus(OrderWork orderWork) {
        orderWorkDao.updateConfirmToInputStatus(orderWork);
    }

    @Override
    public void updateConfirm(OrderWork orderWork) {
        orderWorkDao.updateConfirm(orderWork);
    }

    @Override
    public void updateAssignBack(OrderWork orderWork, Map<String, Boolean> fields) {
        orderWorkDao.updateAssignBack(orderWork, fields);
    }

    @Override
    public void updateToAssignStatus(OrderWork orderWork, Map<String, Boolean> fields) {
        orderWorkDao.updateToAssignStatus(orderWork, fields);
    }

    @Override
    public void updatePartReimburse(OrderWork orderWork) {
        orderWorkDao.updatePartReimburse(orderWork);
    }

    @Override
    public void updateOrderWorkDutyTimeByKey(OrderWork orderWork) {
        if (Objects.nonNull(orderWork.getDutyTime())) {
            orderWork.setDutyStatus(OrderDutyConsts.DUTY_STATUS_SUCCESS);
            orderWork.setNextContactTime(null);
        }
        orderWorkDao.updateOrderWorkDutyTimeByKey(orderWork);
    }

    @Override
    public void updateCleanReworkByKey(OrderWork orderWork) {
        orderWorkDao.updateCleanReworkByKey(orderWork);
    }

    @Override
    public void updateOrderWorkByKey(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkByKey(orderWork);
    }

    @Override
    @OrderWorkChangeSync
    public void updateOrderWorkByKeyForSync(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkByKey(orderWork);
    }

    @Override
    public void updateOrderWorkAddressByKey(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkByKey(orderWork);
    }

    /**
     * 老系统数据同步
     * @param orderWork
     */
    @Override
    public void updateOldSysSync(OrderWork orderWork) {
        orderWorkDao.updateOldSysSync(orderWork);
    }

    @Override
    public void updateOrderWorkAssign(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkAssign(orderWork);
    }

    @Override
    public void updateOrderWorkAssignConfirm(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkAssignConfirm(orderWork);
    }

    @Override
    public int insertOrderWork(OrderWork orderWork) {
        if (StringUtil.isNotBlank(orderWork.getCurrentRemark())){
            orderWork.setCurrentRemark(EmojiUtil.filterEmoji(orderWork.getCurrentRemark()));
        }
        Date createTime = Optional.ofNullable(orderWork.getCreateTime()).orElse(DateUtil.getNow());
        orderWork.setCreateTime(createTime);
        orderWork.setUpdateTime(createTime);
        orderWork.setReceiveTime(createTime);
        return orderWorkDao.insertOrderWork(orderWork);
    }

    @Override
    public void updatePlatWorkById(OrderWork orderWork) {
        orderWorkDao.updatePlatWorkById(orderWork);
    }

    @Override
    public void updateVisitUserId(OrderWork orderWork) {
        orderWorkDao.updateVisitUserId(orderWork);
    }

    @Override
    public void updateCustomerUserId(OrderWork orderWork) {
        orderWorkDao.updateCustomerUserId(orderWork);
    }
    @Override
    public void updateProductIdByKey(OrderWork orderWork) {
        Objects.requireNonNull(orderWork.getWorkId());
        orderWorkDao.updateProductIdByKey(orderWork);
    }

    @Override
    public void updateFinalPriceByKey(OrderWork orderWork) {
        orderWorkDao.updateFinalPriceByKey(orderWork);
    }

    @Override
    public void updateOrderWorkSuspendByKey(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkSuspendByKey(orderWork);
    }

    @Override
    public void updateOrderMasterSupervisorByKey(OrderWork orderWork) {
        orderWorkDao.updateOrderMasterSupervisorByKey(orderWork);
    }

    @Override
    public void updatePartRetainByKey(OrderWork orderWork) {
        orderWorkDao.updatePartRetainByKey(orderWork);
    }

    @Override
    public void updateOrderWorkCompany(OrderWork orderWork) {
        orderWorkDao.updateOrderWorkCompany(orderWork);
    }

    @Override
    public void updateOrderServItemTypeByKey(OrderWork updateOrderWork) {
        orderWorkDao.updateOrderServItemTypeByKey(updateOrderWork);
    }

    @Override
    public void updateOrderWorkDiscountInfo(OrderWork orderWork) {
        Assert.notNull(orderWork.getOrderId(), "orderId不能为空");
        Assert.notNull(orderWork.getWorkId(), "workId不能为空");
        orderWorkDao.updateOrderWorkDiscountInfo(orderWork);
    }
}
